<?php
// +----------------------------------------------------------------------
// | sfadmin - Suifeng Administration
// +----------------------------------------------------------------------
// | Copyright (c) 2017-2020 http://suifeng.tech All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: suifeng <1787585212@qq.com>
// +----------------------------------------------------------------------

namespace app\admin\model;

use suifeng\Model;
use suifeng\admin\AdminOptions;

class Option extends Model
{
    /**
     * 数据库表名。
     *
     * @var string
     */
    protected $name = 'admin_option';

    /**
     * 新增时自动赋值。
     *
     * @var array
     */
    protected $insert = ['is_system' => 0];

    /**
     * 时间戳处理。
     *
     * @var boolean
     */
    protected $autoWriteTimestamp = true;

    public function clear_cache()
    {
        cache('model_admin_option::get_cached_list', null);
    }

    protected function get_cached_list()
    {
        $list = cache('model_admin_option::get_cached_list');

        if (!$list || APP_DEBUG) {
            $order = 'id asc';

            $rows = $this->order($order)->select();

            $list = array();
            foreach ($rows as $row) {
                $list[$row['name']] = $row->getData();
            }

            cache('model_admin_option::get_cached_list', $list);
        }

        return $list;
    }

    public function get_options_array($key)
    {
        $option_list = $this->get_cached_list();
        if (empty($option_list[$key]['content'])) {
            return null;
        }
        return helper('admin/options')->parse($option_list[$key]['content']);
    }
}
